﻿using System.Collections.Generic;

namespace Tomato.Allure
{
	internal class DocumentsImpl : Tomato.Allure.IDocuments
	{
		private EnvDTE.Documents m_instance;

		public DocumentsImpl( EnvDTE.Documents instance )
		{
			m_instance = instance;
		}

		public int Count
		{
			get { return m_instance.Count; }
		}

		public Tomato.Allure.IDocument this[ int index ]
		{
			get
			{
				EnvDTE.Document document = m_instance.Item( index );
				if( document != null )
				{
					return new DocumentImpl( document );
				}

				return null;
			}
		}

		public Tomato.Allure.IDocument this[ string documentName ]
		{
			get
			{
				EnvDTE.Document document = m_instance.Item( documentName );
				if( document != null )
				{
					return new DocumentImpl( document );
				}

				return null;
			}
		}

		public Tomato.Allure.IDocument Add( string kind )
		{
			EnvDTE.Document document = m_instance.Add( kind );
			if( document != null )
			{
				return new DocumentImpl( document );
			}

			return null;
		}

		public void CloseAll( Tomato.Allure.SaveChanges saveChanges )
		{
			EnvDTE.vsSaveChanges closeOption = EnvDTE.vsSaveChanges.vsSaveChangesNo;
			switch( saveChanges )
			{
				//case Allure.SaveChanges.No: closeOption = vsSaveChanges.vsSaveChangesNo; break;
				case Tomato.Allure.SaveChanges.Prompt: closeOption = EnvDTE.vsSaveChanges.vsSaveChangesPrompt; break;
				case Tomato.Allure.SaveChanges.Yes: closeOption = EnvDTE.vsSaveChanges.vsSaveChangesYes; break;
			}

			m_instance.CloseAll( closeOption ); 
		}

		public Tomato.Allure.IDocument Open( string documentFilePath, string kind, bool bReadOnly )
		{
			EnvDTE.Document document = m_instance.Open( documentFilePath, kind, bReadOnly );
			if( document != null )
			{
				return new DocumentImpl( document );
			}

			return null;
		}

		public void SaveAll()
		{
			m_instance.SaveAll();
		}

		public IEnumerator<Tomato.Allure.IDocument> GetEnumerator()
		{
			for( int i = 1 ; i <= m_instance.Count ; ++i )
			{
				EnvDTE.Document document = m_instance.Item( i );
				if( document != null )
				{
					yield return new DocumentImpl( document );
				}
				else
				{
					yield return null;
				}
			}
		}

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}
	}
}
